package com.umeox.watch.worshiplearn.ui

import android.os.Bundle
import android.util.LayoutDirection
import androidx.core.text.TextUtilsCompat
import androidx.viewpager2.widget.ViewPager2
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.moto.watch.themes.widget.ImageView
import com.umeox.moto.watch.themes.widget.TextView
import com.umeox.watch.worshiplearn.R
import com.umeox.watch.worshiplearn.adapter.ViewPagerAdapter
import java.util.Locale

/**
 * Created by Rambo 2022/9/10
 * Describe:
 */
class WuduViewPagerActivity : ThemeAppCompatActivity() {

    companion object {
        const val GENDER_EXTRA = "gender_extra"
        const val GENDER_BOY: Int = 1
        const val GENDER_GIRL: Int = 2

    }

    private lateinit var mViewPager: ViewPager2
    private lateinit var mIndexTv: TextView
    private lateinit var mBtnLast: ImageView
    private lateinit var mBtnNext: ImageView

    private var gender: Int? = null

    private lateinit var dataList: ArrayList<Int>

    private var mCurrentItemIndex: Int = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.wudu_viewpager_activity)
        gender = intent?.getIntExtra(GENDER_EXTRA, GENDER_BOY)
        initView()
        initData()
    }

    private fun initView() {
        mViewPager = findViewById(R.id.vp)
        mIndexTv = findViewById(R.id.tv_index)

        if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL) {
            mBtnLast = findViewById(R.id.iv_right)
            mBtnNext = findViewById(R.id.iv_left)
        } else {
            mBtnLast = findViewById(R.id.iv_left)
            mBtnNext = findViewById(R.id.iv_right)
        }
    }


    private fun initData() {
        val imagerRes = if (gender == GENDER_BOY) {
            resources.getStringArray(R.array.wudu_boy_images)
        } else {
            resources.getStringArray(R.array.wudu_girl_images)
        }
        dataList = ArrayList()

        for (res in imagerRes) {
            val resId = resources.getIdentifier(res, "drawable", packageName)
            dataList.add(resId)
        }

        mViewPager.adapter = ViewPagerAdapter(dataList, R.layout.item_pager_pary_wudu)
        mCurrentItemIndex = 0
        mViewPager.currentItem = mCurrentItemIndex
        mViewPager.registerOnPageChangeCallback(ViewPageChange())
        mBtnLast.setOnClickListener {
            mViewPager.currentItem = mCurrentItemIndex - 1
        }

        mBtnNext.setOnClickListener {
            mViewPager.currentItem = mCurrentItemIndex + 1
        }


    }

    inner class ViewPageChange : ViewPager2.OnPageChangeCallback() {

        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            mCurrentItemIndex = position
            mIndexTv.text = String.format(Locale.getDefault(), "%d", position + 1)
            when (position) {
                0 -> {
                    mBtnLast.isEnabled = false
                    mBtnLast.alpha = 0f
                }

                (dataList.size - 1) -> {
                    mBtnNext.isEnabled = false
                    mBtnNext.alpha = 0f
                }

                else -> {
                    mBtnLast.isEnabled = true
                    mBtnNext.isEnabled = true
                    mBtnLast.alpha = 1f
                    mBtnNext.alpha = 1f
                }
            }
        }
    }

}
